package com.koron.css2.clientCenter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.PubReceiptBean;
import com.koron.css2.baseConfig.mapper.*;
import com.koron.css2.baseConfig.queryBean.BankQueryBean;
import com.koron.css2.baseConfig.queryBean.PubReceiptQueryBean;
import com.koron.css2.baseConfig.queryBean.WaterTypeQueryBean;
import com.koron.css2.baseConfig.vo.BankVO;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.baseConfig.vo.WaterTypeVO;
import com.koron.css2.clientCenter.bean.ReadCycleConfigBean;
import com.koron.css2.clientCenter.bean.UserChangeItemBean;
import com.koron.css2.clientCenter.mapper.ChangeUserImportMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.ChangeUserItemsQueryBean;
import com.koron.css2.clientCenter.vo.ChangeUserImportVo;
import com.koron.css2.serviceManage.bean.DistrictBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.DistrictMapper;
import com.koron.css2.serviceManage.mapper.PubAdminAreaMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ChangeUserImportQuery implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		ChangeUserItemsQueryBean bean = JsonUtils.objectToPojo(req.getData(), ChangeUserItemsQueryBean.class);
		try {
			ChangeUserImportMapper mapper = factory.getMapper(ChangeUserImportMapper.class);
			ChangeUserImportVo vo = mapper.getBillByid(bean.getId());
			
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<UserChangeItemBean> items = mapper.getChangeItems(bean, 2);
			Map<String, Object> mapData = new HashMap<String, Object>();
			dictTrunToValue(factory, userInfo, items);
			mapData.put("bill", vo);
			
			mapData.put("items",new PageInfo<>(items));
			@SuppressWarnings("rawtypes")
			MessageBean<Map> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", Map.class);
			info.setData(mapData);
			return info;
		} catch (Exception e) {
			logger.error("批量变更列表查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

    public List<UserChangeItemBean> dictTrunToValue(SessionFactory factory, UserInfoBean userInfo, List<UserChangeItemBean> list) {
        //数据字典：1.客户类型 2.证件类型 3.信用等级 4.账户类型 5.投递方式 6.发票类型  7.抄表周期  8.装表位置 9.水表用途 10.用水对象 11.缴费方式
        //账户：1.开户银行
        //用户：1.行政区域 2.营业区域 3.营业所 4.行业分类 5.用水类型 6.优惠策略
        //1.水表类型
        //String [] codes = {"CTT","IDT","CDL","ACT","SDW","BBT","RTP","CTL","CMC","UWS","BPW","CUS","WMT"};
        //dictionary.findDictionaryByCodes(codes);
        Map<String, String> CTT = Tools.mapDicByCode(factory, "CTT");
        Map<String, String> IDT = Tools.mapDicByCode(factory, "IDT");
        Map<String, String> CDL = Tools.mapDicByCode(factory, "CDL");
        Map<String, String> ACT = Tools.mapDicByCode(factory, "ACT");
        Map<String, String> SDW = Tools.mapDicByCode(factory, "SDW");
        Map<String, String> BBT = Tools.mapDicByCode(factory, "BBT");

        List<ReadCycleConfigBean> readCycleConfigList = factory.getMapper(UserInfoMapper.class).readCycleConfigList();
        Map<String, String> RTP = readCycleConfigList.stream().collect(Collectors.toMap(ReadCycleConfigBean::getId, ReadCycleConfigBean::getName));

        Map<String, String> CTL = Tools.mapDicByCode(factory, "CTL");
        Map<String, String> CMC = Tools.mapDicByCode(factory, "CMC");
        Map<String, String> UWS = Tools.mapDicByCode(factory, "UWS");
        Map<String, String> BPW = Tools.mapDicByCode(factory, "BPW");
        Map<String, String> CUS = Tools.mapDicByCode(factory, "CUS");
        Map<String, String> MMT = Tools.mapDicByCode(factory, "MMT");//水表类型
        //银行信息
        BankMapper bankMapper = factory.getMapper(BankMapper.class);
        BankQueryBean bankBean = new BankQueryBean();
        bankBean.setIsLeaf(1);
        List<BankVO> bankList = bankMapper.findBank(bankBean);
        //行政区域
        DistrictMapper districtMapper = factory.getMapper(DistrictMapper.class);
//		DistrictBean dbean = new DistrictBean();
//		dbean.setCode("all");
//		List<DistrictBean> distList = districtMapper.findDistrict(dbean);
        //营业区域
        BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
        List<SelectVO> businessAreaList = businessAreaMapper.select(null, userInfo.getCurWaterCode());
        //营业所
        BusinessAbodeMapper busniessAbodemapper = factory.getMapper(BusinessAbodeMapper.class);
        List<SelectVO> busniessAbodeList = busniessAbodemapper.businessAbodeSelect(userInfo.getCurWaterCode());
        //行业分类
        TradeClassifyMapper tradeMapper = factory.getMapper(TradeClassifyMapper.class);
        List<SelectVO> tradeList = tradeMapper.tradeClassifySelect();
        //用水类型
        WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);
        WaterTypeQueryBean waterQuery = new WaterTypeQueryBean();
        waterQuery.setStatus(1);
        List<WaterTypeVO> waterTypeList = waterTypeMapper.findWaterType(waterQuery);
        //优惠策略
        PrefStrategyMapper prefStrategyMapper = factory.getMapper(PrefStrategyMapper.class);
        List<SelectVO> prefStrategyList = prefStrategyMapper.prefStrategySelect();
        //单据类型
        PubReceiptMapper mapper = factory.getMapper(PubReceiptMapper.class);
        List<PubReceiptBean> receiptBean = mapper.selectPubReceiptList(new PubReceiptQueryBean());
        PubAdminAreaMapper pmapper = factory.getMapper(PubAdminAreaMapper.class,"_default");
        
        int i = 0;
        for (UserChangeItemBean bean : list) {
            for (PubReceiptBean pbean : receiptBean) {
                if (pbean.getReceiptNo().equals(bean.getReceiptType())) {
                    bean.setReceiptType(pbean.getReceiptName());
                    break;
                }
            }
            switch (bean.getChangeItem()) {
                case "客户类型":
                    bean.setOldItemValue(CTT.get(bean.getOldItemValue()));
                    bean.setNewItemValue(CTT.get(bean.getNewItemValue()));
                    break;
                case "证件类型":
                    bean.setOldItemValue(IDT.get(bean.getOldItemValue()));
                    bean.setNewItemValue(IDT.get(bean.getNewItemValue()));
                    break;
                case "信用等级":
                    bean.setOldItemValue(CDL.get(bean.getOldItemValue()));
                    bean.setNewItemValue(CDL.get(bean.getNewItemValue()));
                    break;
                case "账户类型":
                    bean.setOldItemValue(ACT.get(bean.getOldItemValue()));
                    bean.setNewItemValue(ACT.get(bean.getNewItemValue()));
                    break;
                case "投递方式":
                    bean.setOldItemValue(SDW.get(bean.getOldItemValue()));
                    bean.setNewItemValue(SDW.get(bean.getNewItemValue()));
                    break;
                case "发票类型":
                    bean.setOldItemValue(BBT.get(bean.getOldItemValue()));
                    bean.setNewItemValue(BBT.get(bean.getNewItemValue()));
                    break;
                case "抄表周期":
                    bean.setOldItemValue(RTP.get(bean.getOldItemValue()));
                    bean.setNewItemValue(RTP.get(bean.getNewItemValue()));
                    break;
                case "装表位置":
                    bean.setOldItemValue(CTL.get(bean.getOldItemValue()));
                    bean.setNewItemValue(CTL.get(bean.getNewItemValue()));
                    break;
                case "水表用途":
                    bean.setOldItemValue(CMC.get(bean.getOldItemValue()));
                    bean.setNewItemValue(CMC.get(bean.getNewItemValue()));
                    break;
                case "用水对象":
                    bean.setOldItemValue(UWS.get(bean.getOldItemValue()));
                    bean.setNewItemValue(UWS.get(bean.getNewItemValue()));
                    break;
                case "缴费方式":
                    bean.setOldItemValue(BPW.get(bean.getOldItemValue()));
                    bean.setNewItemValue(BPW.get(bean.getNewItemValue()));
                    break;
                case "用户状态":
                    bean.setOldItemValue(CUS.get(bean.getOldItemValue()));
                    bean.setNewItemValue(CUS.get(bean.getNewItemValue()));
                    break;
                case "水表类型":
                    bean.setOldItemValue(MMT.get(bean.getOldItemValue()));
                    bean.setNewItemValue(MMT.get(bean.getNewItemValue()));
                    break;
                case "购买方开户行":
                case "开户银行":
                    i = 0;
                    for (BankVO bbean : bankList) {
                        if (bbean.getId().equals(bean.getOldItemValue())) {
                            bean.setOldItemValue(bbean.getBankName());
                            i += 1;
                        }
                        if (bbean.getId().equals(bean.getNewItemValue())) {
                            bean.setNewItemValue(bbean.getBankName());
                            i += 1;
                        }
                        if (i == 2) {
                            break;
                        }
                    }
                    break;
                case "行政区域":
                    i = 0;
                    DistrictBean abean = districtMapper.findDistrictById(bean.getOldItemValue());
                    DistrictBean fbean = districtMapper.findDistrictById(bean.getNewItemValue());
                    bean.setOldItemValue(abean == null ? "" : abean.getName());
                    bean.setNewItemValue(fbean == null ? "" : fbean.getName());
                    break;
                case "营业区域":
                    i = 0;
                    for (SelectVO bbean : businessAreaList) {
                        if (bbean.getId().equals(bean.getOldItemValue())) {
                            bean.setOldItemValue(bbean.getName());
                            i += 1;
                        }
                        if (bbean.getId().equals(bean.getNewItemValue())) {
                            bean.setNewItemValue(bbean.getName());
                            i += 1;
                        }
                        if (i == 2) {
                            break;
                        }
                    }
                    break;
                case "营业所":
                    i = 0;
                    for (SelectVO bbean : busniessAbodeList) {
                        if (bbean.getId().equals(bean.getOldItemValue())) {
                            bean.setOldItemValue(bbean.getName());
                            i += 1;
                        }
                        if (bbean.getId().equals(bean.getNewItemValue())) {
                            bean.setNewItemValue(bbean.getName());
                            i += 1;
                        }
                        if (i == 2) {
                            break;
                        }
                    }
                    break;
                case "行业分类":
                    i = 0;
                    for (SelectVO bbean : tradeList) {
                        if (bbean.getId().equals(bean.getOldItemValue())) {
                            bean.setOldItemValue(bbean.getName());
                            i += 1;
                        }
                        if (bbean.getId().equals(bean.getNewItemValue())) {
                            bean.setNewItemValue(bbean.getName());
                            i += 1;
                        }
                        if (i == 2) {
                            break;
                        }
                    }
                    break;
                case "用水类型":
                    i = 0;
                    for (WaterTypeVO bbean : waterTypeList) {
                        if (bbean.getId().equals(bean.getOldItemValue())) {
                            bean.setOldItemValue(bbean.getWaterTypeName());
                            i += 1;
                        }
                        if (bbean.getId().equals(bean.getNewItemValue())) {
                            bean.setNewItemValue(bbean.getWaterTypeName());
                            i += 1;
                        }
                        if (i == 2) {
                            break;
                        }
                    }
                    break;
                case "优惠策略ID":
                    i = 0;
                    for (SelectVO bbean : prefStrategyList) {
                        if (bbean.getId().equals(bean.getOldItemValue())) {
                            bean.setOldItemValue(bbean.getName());
                            i += 1;
                        }
                        if (bbean.getId().equals(bean.getNewItemValue())) {
                            bean.setNewItemValue(bbean.getName());
                            i += 1;
                        }
                        if (i == 2) {
                            break;
                        }
                    }
                    break;
                case "增值税标记":
                    bean.setOldItemValue("1".equals(bean.getOldItemValue()) ? "是" : "否");
                    bean.setNewItemValue("1".equals(bean.getNewItemValue()) ? "是" : "否");
                    break;
                case "装表结构地址":
                	
                	String old = pmapper.getNameById(bean.getOldItemValue());
                	String newv = pmapper.getNameById(bean.getNewItemValue());
                    bean.setOldItemValue(old==null?"无":old);
                    bean.setNewItemValue(newv==null?"无":newv);
                    break;
                case "客户结构地址":
                	String old1 = pmapper.getNameById(bean.getOldItemValue());
                	String newv1 = pmapper.getNameById(bean.getNewItemValue());
                    bean.setOldItemValue(old1==null?"无":old1);
                    bean.setNewItemValue(newv1==null?"无":newv1);
                    break;
                default:
                    break;
            }
            if (ObjectUtil.isEmpty(bean.getOldItemValue()) || ("空").equals(bean.getOldItemValue()) ) {
                bean.setOldItemValue("无");
            }
        }
        return list;
    }
}
